// pages/shoppingCart/shoppingCart.js
Page({
  data: {
    goodsItem: [],
    isNotice:true,
    // isChecked:false,
    isAllChecked:false,
    accounts:0,
    notice:'特殊时期，即日起下单时间调整为最早当日22点半至最晚次日20点。订单可能会延迟送达或被约满，请谅解~',
    cart:[]
  },
  onLoad:function(){
    if (this.__data__.notice == ''){
      this.setData({
        isNotice: false
      })
    }
    var that=this;
    wx.request({
      url: 'http://127.0.0.1:8080/cart',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          cart:res.data.cart,
          accounts: res.data.account
        })
      }
    })
    wx.request({
      url: 'http://127.0.0.1:8080/queryComponentGoods',
      data: {viewId:'cart'},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          goodsItem: res.data.componentGoodsList
        })
      }
    })
  },
  // onReady:function(){
  //   var arr = [];
  //   var cart = this.__data__.cart;
  //   for (var i = 0; i < cart.length; i++) {
  //     if (cart[i].checked == true) {
  //       arr.push(cart[i]);
  //     }
  //   }
  //   console.log(arr)
  //   console.log('onShow')
  // },
  isAllChecked:function(){
    var cart = this.__data__.cart;
    var money = 0;
    if (this.__data__.isAllChecked == false){
      for(var i=0;i<cart.length;i++){
        money += cart[i].price * cart[i].nums
      }
    }else{
   
    }
    var arr=[];
    for(var i=0;i<cart.length;i++){
      this.__data__.cart[i].checked = !this.data.isAllChecked;
      arr.push(cart[i])
    }
    this.setData({
      isAllChecked:(!this.data.isAllChecked),
      accounts: money,
      cart: arr
    })
  },
  deletNotice:function(){
    this.setData({
      isNotice:false,
    })
  },
  cart_head_modify_sub:function(e){
    var id = e.target.dataset.id;
    var nums = this.__data__.cart[id].nums;
    nums--;
    let cartNum = 'cart[' + id + '].nums';
      var money = 0;
    if (this.__data__.isAllChecked == true) {
      money = this.__data__.cart[id].price * (this.__data__.cart[id].nums - 1)
      for (var i = 0; i < this.__data__.cart.length; i++) {
        if (i != id) {
          money += this.__data__.cart[i].price * this.__data__.cart[i].nums
        }
      }
    }
    if (this.__data__.cart[id].checked == true) {
      money = this.__data__.accounts - this.__data__.cart[id].price
    }
    this.setData({
      [cartNum]: nums,
      accounts: money
    })
  },
  cart_head_modify_add:function(e){
    var id = e.target.dataset.id;
    var nums = this.__data__.cart[id].nums;
    nums++;
    let cartNum = 'cart[' + id + '].nums';
    var money = 0;
    if (this.__data__.isAllChecked == true){
      money = this.__data__.cart[id].price * (this.__data__.cart[id].nums+1)
      for(var i=0;i<this.__data__.cart.length;i++){
        if(i !=id){
          money += this.__data__.cart[i].price * this.__data__.cart[i].nums
        }
      }
    }
    if (this.__data__.cart[id].checked == true){
      money = this.__data__.accounts + this.__data__.cart[id].price
    }
    this.setData({
      [cartNum]: nums,
      accounts: money
    })
  },
  isChecked:function(e){
    var money=0;
    var id = e.target.dataset.id;
    let checked = 'cart[' + id + '].checked';
    if (this.__data__.isAllChecked == true){
      if (this.__data__.cart[id].checked == true){
        money = this.__data__.accounts - (this.__data__.cart[id].nums * this.__data__.cart[id].price);
      }else{
        money = this.__data__.accounts + (this.__data__.cart[id].nums * this.__data__.cart[id].price);
      }
    }else{
        if (this.__data__.cart[id].checked == false){
          money = this.__data__.accounts + (this.__data__.cart[id].nums * this.__data__.cart[id].price);
        }else{
          money = this.__data__.accounts - (this.__data__.cart[id].nums * this.__data__.cart[id].price);
        }
      }
    this.setData({
      isAllChecked:false,
      accounts:money,
      [checked]: !this.__data__.cart[id].checked
    })
  }
})